package com.bk.service.impl;
import com.bk.constant.MessageConstant;
import com.bk.constant.StatusConstant;
import com.bk.dto.UserDTO;
import com.bk.dto.UserLoginDTO;
import com.bk.dto.UserRegisterDTO;
import com.bk.entity.User;
import com.bk.exception.AccountLockedException;
import com.bk.exception.AccountNotFoundException;
import com.bk.exception.PasswordErrorException;
import com.bk.mapper.*;
import com.bk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
    //用户
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @Override
    public User login(UserLoginDTO userLoginDTO) {
        //获取前端用户名和密码
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();

        User user = userMapper.getByUsername(username);
        //查询出用户为空
        if (user == null)
        {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        if (!password.equals(user.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        //账号锁定
        if (user.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }


        return user;
    }

    /**
     * 用户注册
     * @param userRegisterDTO
     */
    @Override
    public void register(UserRegisterDTO userRegisterDTO) {
        //1.接受输入的用户名密码

        //2.判断是否重复的用户名,如果是，抛出异常

        //3.添加用户


    }

    /**
     * 修改用户信息
     * @param userDTO
     */
    @Override
    public void updateMessage(UserDTO userDTO) {
        //1.将dto中的数据复制到user中

        //2.BaseContext获取userid

        //3.调用mapper对user表进行修改


    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Override
    public User getById(Long id) {
        //1.根据id查表

        //2.返回

        return null;
    }

    /**
     * 修改密码
     */
    public void updatePassword(String username ,String password) {
       //1.检查用户名是否存在，不存在抛出异常

        //2.完成邮箱验证

        //3.修改
    }

    @Override
    public void comment(Long innovationId, String content) {
        //1.创建评论对象补充数据

        //2.执行评论添加

    }
}
